package org.apache.lucene.index;

import java.io.IOException;
import java.util.Arrays;

import org.apache.lucene.store.IndexOutput;

public class TagBlockSkipListWriter extends MultiLevelSkipListWriter {
  private int[] lastSkipDoc;
  private long[] lastSkipPointer;

  private IndexOutput freqOutput;

  private int curDoc;
  private long curFreqPointer;

  TagBlockSkipListWriter(int skipInterval, int numberOfSkipLevels, int docCount, IndexOutput freqOutput) {
    super(skipInterval, numberOfSkipLevels, docCount);
    this.freqOutput = freqOutput;

    lastSkipDoc = new int[numberOfSkipLevels];
    lastSkipPointer = new long[numberOfSkipLevels];
  }

  void setSkipData(int doc) {
    this.curDoc = doc;
    this.curFreqPointer = freqOutput.getFilePointer();
  }

  protected void resetSkip() {
    super.resetSkip();
    Arrays.fill(lastSkipDoc, 0);
    Arrays.fill(lastSkipPointer, freqOutput.getFilePointer());
  }

  protected void writeSkipData(int level, IndexOutput skipBuffer) throws IOException {
    skipBuffer.writeVInt(curDoc - lastSkipDoc[level]);
    skipBuffer.writeVInt((int) (curFreqPointer - lastSkipPointer[level]));

    lastSkipDoc[level] = curDoc;
    // System.out.println("write doc at level " + level + ": " + curDoc);
    
    lastSkipPointer[level] = curFreqPointer;
  }
}
